package net.heri.xmppchat.activities.contactlist;

import net.heri.xmppchat.R;
import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.google.inject.Inject;

@ContentView(R.layout.contact_list_activity)
public class ContactListActivity extends RoboActivity {
	
	private static final String TAG = "contactlist";
	
	@InjectView(R.id.edt_contact_list_search)
	private	EditText				mEdtSearch;
	@InjectView(R.id.contact_list)
	private ListView				mListContact;
	@InjectView(android.R.id.empty)
	private	View					mLayoutEmpty;
	@Inject
	private ContactListAdapter		mAdapter;
	@Inject
	private	ContactListController	mController;
	
	private Handler 				mHandler;
	
	
	
	/**
	 * @see roboguice.activity.RoboActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getController().setHandler(getHandler() );
		getListContact().setAdapter(getAdapter() );
		showEmptyMessage((getAdapter().getCount() < 1)  );
		
		getEdtSearch().addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence cs, int start, int before, int count) {
				getAdapter().getFilter().filter(cs);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
	}
	
	// ----------------------------------------	//
	//	M I S C E L L A N E O U S				//
	// ----------------------------------------	//
	
	/**
	 * 
	 */
	private void showEmptyMessage(boolean isShow) throws NullPointerException {
		if(getLayoutEmpty() == null) {
			throw new NullPointerException("View android.R.id.empty is null !");
		}
		
		getLayoutEmpty().setVisibility(isShow ? View.VISIBLE : View.GONE);
	}
	
	
	// ----------------------------------------	//
	//	S E T T E R   G E T T E R				//
	// ----------------------------------------	//
	
	public EditText getEdtSearch() {
		return mEdtSearch;
	}

	public ListView getListContact() {
		return mListContact;
	}

	public View getLayoutEmpty() {
		return mLayoutEmpty;
	}

	public ContactListAdapter getAdapter() {
		return mAdapter;
	}

	public Handler getHandler() {
		if(mHandler == null) {
			mHandler = new Handler(){
				public void handleMessage(Message message) {
					switch(message.what) {
						
					}
				};
			};
		}
		
		return mHandler;
	}

	public ContactListController getController() {
		return mController;
	}
	
	
	
}
